<?php

/*
*/

class Purchase_Application_model extends CI_Model
{

	static $table;
	public $error;

	function __construct()
	{
		parent::__construct();
		//载入库文件
		$this->load->library('test_database');
		$this->test_database->test();
		//载入核心类
		$this->load->database($this->test_database->get_db());
		//载入库文件
		$this->load->library('form_validation');
		//载入本地语言文件
		$this->lang->load('form_validation','chinese');
		//初始化
		$this->table = 'purchase_applications';

		$this->db->trans_strict(FALSE);
	}

	public function create()
	{
		$sub_goods_ids = $this->input->post('sub_goods_ids');
		$quantities = $this->input->post('quantities');
		if (count($sub_goods_ids) == count($quantities))
		{
			$data['pur_app_goods'] = '[';
			foreach ($sub_goods_ids as $key=>$value)
			{
				$data['pur_app_goods'] .= '{"sub_goods_id":"' . $value . '","quantity":"' . $quantities[$key] . '"},';
			}
			$data['pur_app_goods'] = substr($data['pur_app_goods'], 0, -1) . ']';
			$data['pur_app_number'] = date("YmdHis", time()) . substr(time(), -6);
			$data['pur_app_person'] = $this->input->cookie('realname');
			$data['pur_app_user_id'] = $this->input->cookie('user_id');
			$data['pur_app_status'] = 1;
			$data['create_date'] = time();
			$this->db->trans_begin();
			$this->db->insert($this->table, $data);
			if ($this->db->affected_rows() == 1)
			{
				$this->db->trans_commit();
				return true;
			}
			else
			{
				$this->db->trans_rollback();
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	public function get_list($start, $limit, $orderby, $keyword=false)
	{
		$this->db->limit($limit, $start);
		$this->db->order_by('create_date', 'DESC');
		$query = $this->db->get($this->table);
		return $query->result();
	}

	public function get_all($get_total_rows=false)
	{
		$query = $this->db->get($this->table);
		if ($get_total_rows)
		{
			return $query->num_rows();	//[String:Number]
		}
		else
		{
			return $query->result();	//[Object]
		}
	}

	public function get_one($pur_app_id=0)
	{
		if (0 == $pur_app_id)
		{
			$pur_app_id = $this->input->post('pur_app_id');
		}
		if ($pur_app_id)
		{
			$this->db->where('pur_app_id', $pur_app_id);
			$query = $this->db->get($this->table);
			if ($query->num_rows() == 1)
			{
				foreach ($query->result() as $data)
				{
					$data = get_object_vars($data);
					$data['pur_app_goods'] = json_decode($data['pur_app_goods']);
					return $data;
				}
			}
			else
			{
				$this->error = '不存的采购申请';
				return false;
			}
		}
		else
		{
			$this->error = '请提交正确的请求';
			return false;
		}
	}

	public function set_status($status)
	{
		$pur_app_id = $this->input->post('pur_app_id');
		if ($pur_app = $this->get_one($pur_app_id))
		{
			switch ($status)
			{
				case 1:

					break;
				case 2:
					$data = array('pur_app_status' => 2);
					$this->db->where('pur_app_id', $pur_app_id);
					$this->db->update($this->table, $data);
					break;
				case 3:
					if (2 != $pur_app['pur_app_status'])
					{
						$data = array('pur_app_status' => 3);
						$this->db->where('pur_app_id', $pur_app_id);
						$this->db->update($this->table, $data);
					}
					else
					{
						$this->error = '采购申请已经被通过';
					}
					break;
				default:
					$this->error = __FUNC__ . 'KOWN ERORR ON LINE' . __LINE__;
			}
		}
		else
		{
			$this->error = '不存在的采购申请';
		}
		return false;
	}

}